package com.cema.manage.zfb.impl;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayFundTransUniTransferModel;
import com.alipay.api.domain.Participant;
import com.alipay.api.request.AlipayFundTransUniTransferRequest;
import com.alipay.api.response.AlipayFundTransUniTransferResponse;
import com.cema.manage.project.enums.RedisEnum;
import com.cema.manage.project.manage.users.domain.Users;
import com.cema.manage.redis.RedisService;
import com.cema.manage.zfb.AlipayFundTransUniTService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class AlipayFundTransUniTServiceImpl implements AlipayFundTransUniTService {
    @Resource
    private RedisService redisService;

    @Override
    public AlipayFundTransUniTransferResponse alipaySingle(Users users, String orderId, String amount, String mark,String uvChannel) throws AlipayApiException {
        // 初始化SDK
        AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig(uvChannel));

        // 构造请求参数以调用接口
        AlipayFundTransUniTransferRequest request = new AlipayFundTransUniTransferRequest();
        AlipayFundTransUniTransferModel model = new AlipayFundTransUniTransferModel();

        // 设置商家侧唯一订单号
        model.setOutBizNo(orderId);

        // 设置订单总金额
        model.setTransAmount(amount);

        // 设置描述特定的业务场景
        model.setBizScene("DIRECT_TRANSFER");

        // 设置业务产品码
        model.setProductCode("TRANS_ACCOUNT_NO_PWD");

        // 设置收款方信息
        Participant payeeInfo = new Participant();
        //payeeInfo.setCertType("ALIPAY_LOGON_ID");
        //payeeInfo.setCertNo("1201152******72917");
        payeeInfo.setIdentity(users.getAfbPhone());
        payeeInfo.setName(users.getAfbName());
        payeeInfo.setIdentityType("ALIPAY_LOGON_ID");
        model.setPayeeInfo(payeeInfo);
        // 设置业务备注
        model.setRemark(mark);

        // 设置转账业务请求的扩展参数
        model.setBusinessParams("{\"payer_show_name_use_alias\":\"true\"}");

        request.setBizModel(model);
        AlipayFundTransUniTransferResponse response = alipayClient.certificateExecute(request);
        System.out.println(response.getBody());
        return response;
//        if (response.isSuccess()) {
//            System.out.println("调用成功");
//            return response.getPayFundOrderId();
//        } else {
//            System.out.println("调用失败");
//            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
//            //String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
//            // System.out.println(diagnosisUrl);
//            return response.getSubMsg();
//        }
    }

    private AlipayConfig getAlipayConfig(String uvChannel) {
        String privateKey = (String) redisService.get(RedisEnum.ZFB_SECRET_KEY.getKey()+uvChannel);
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setServerUrl((String) redisService.get(RedisEnum.ZFB_URL.getKey()+uvChannel));
        alipayConfig.setAppId((String) redisService.get(RedisEnum.ZFB_APP_ID.getKey()+uvChannel));
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");
        alipayConfig.setFormat("json");
        alipayConfig.setAppCertPath((String) redisService.get(RedisEnum.ZFB_APP_CERT_PATH.getKey()+uvChannel));
        alipayConfig.setAlipayPublicCertPath((String) redisService.get(RedisEnum.ZFB_ALIPAY_PUBLIC_CERT_PATH.getKey()+uvChannel));
        alipayConfig.setRootCertPath((String) redisService.get(RedisEnum.ZFB_ROOT_CERT_PATH.getKey()+uvChannel));
        return alipayConfig;
    }
}
